
local msamacro3="la chi sandiego denver sanfran dc vegas"
foreach q in `msamacro3' {
local msamacro="2008 2009 2010 2011 2012"
local msamacro2="KG KK KQ KX"
foreach j in `msamacro' {
foreach k in `msamacro2' {
use housingfutures_`q',clear
keep H0_FUPJHERC_RMS*_`k'`j'_N_B date
rename  H0_FUPJHERC_RMS*_`k'`j'_N_B future
sort date
*sometimes there are holidays; take the day before
replace future=future[_n-1] if future==. & future[_n+1]!=.
drop if future==.
save tt`j'`k'.dta,replace
}
}

use tt2008KG.dta
local msamacro2="KK KQ KX"
foreach k in `msamacro2' {
append using tt2008`k'.dta
}

local msamacro="2009 2010 2011 2012"
local msamacro2="KG KK KQ KX"
foreach j in `msamacro' {
foreach k in `msamacro2' {
append using tt`j'`k'.dta
}
}

gen yeart=year(date)
gen montht=month(date)
gen dayt=day(date)
gen numdatet=mdy(montht,dayt,yeart)
gen weekday=dow(numdatet)


gen temp=abs(numdatet-numdatet[_n-1])
gen new=numdatet if temp>5
replace new=numdatet in 1
replace new=new*_n
replace new=new[_n-1] if new==.


sort new numdatet
by new: gen error=future[_N]/future-1
replace error=error[_n-1] if error==.


by new: gen my=(yeart[_N]-1988)*12+montht[_N]-2
keep if weekday==5
by new: gen lag=(numdatet[_N]-numdatet[_n]+7)/7
keep my lag error
reshape wide error, i(my) j(lag)
gen city="`q'"
sort my city
save `q'futures.dta,replace

}

